<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="el-form-login"
          :rules="foreRules"
          :model="userInfo"
          ref="elFormRef"
          @keydown.enter="login"
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              type="text"
              :prefix-icon="User"
              v-model="userInfo.username"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              show-password
              :prefix-icon="Lock"
              v-model="userInfo.password"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              class="el-button-login"
              @click="login"
              :loading="loading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive } from 'vue'
import { ref } from 'vue'
import useUserStore from '@/store/modules/user.ts'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time.ts'

let userInfo = reactive({ username: 'admin', password: '111111' })
let userStore = useUserStore()
let $router = useRouter()
let $route = useRoute()
let loading = ref(false)
let elFormRef: any = ref()

const validateUsername = (_rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入账号'))
  } else {
    if (value.length < 5 || value.length > 10) {
      callback(new Error('账号长度必须为6到10位'))
    }
    callback()
  }
}
const validatePassword = (_rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (value.length < 6 || value.length > 15) {
      callback(new Error('密码长度必须为6到15位'))
    }
    callback()
  }
}

let foreRules = reactive({
  username: [
    // { required: true, min: 6, max: 10, message: '账号长度必须为6到10位', trigger: 'change' }
    { validator: validateUsername, trigger: 'change' },
  ],
  password: [
    // { required: true,min: 6, max: 15, message: '密码长度必须为6到15位', trigger: 'change' }
    { validator: validatePassword, trigger: 'change' },
  ],
})

async function login() {
  // 通过pinia的action函数去处理逻辑

  loading.value = true
  try {
    // 表单校验函数会返回一个promise对象，await不通过则会报错
    await elFormRef.value.validate()
    // 发送axios请求
    await userStore.loginAction(userInfo)
    $router.replace({
      path: ($route.query.redirect as string) || '/home',
    })
    ElNotification.success({
      message: '欢迎登录',
      title: `Hi,${getTime()}`,
    })
  } catch (error: any) {
    ElNotification.error({
      message:
        (error as Error).message || error[Object.keys(error)[0]][0].message,
    })
  }
  loading.value = false
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100wh;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .el-form-login {
    width: 80%;
    padding: 20px;
    border: 1px $login-border-color;
    border-radius: 10px;
    box-shadow: 0 0 10px $login-border-color;
    background-image: linear-gradient(to top, #7fa2e0, #84d8e7);
    position: relative;
    top: 30vh;
  }

  h1 {
    font-size: 40px;
    color: white;
  }

  h2 {
    font-size: 20px;
    color: white;
    margin: 10px 0;
  }

  .el-button-login {
    width: 100%;
  }
}
</style>
